package com.wangchao.administrator.zenmechi.ui.stepactivity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.bean.stepbean.StepFoodmaterialsBean;
import com.wangchao.administrator.zenmechi.image.ImageHepler;
import com.wangchao.administrator.zenmechi.ui.fragment.userfragment.userMsg;
import com.wangchao.administrator.zenmechi.ui.loginactivity.LoginActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/18.
 */
public class MyAdapterForMaterial extends BaseAdapter {

    private Context context;
    private List<StepFoodmaterialsBean> list;
    private boolean flag = false;

    public MyAdapterForMaterial(Context context, List<StepFoodmaterialsBean> list) {
        this.context = context;
        this.list = list;
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(userMsg userMsg){
        if (!TextUtils.isEmpty(userMsg.getMsg())){
            flag = true;
        }else {
            flag =false;
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = LayoutInflater.from(context).
                    inflate(R.layout.step_material_list_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

        ImageHepler.showImage(context,list.get(i).getPic(),holder.imageMaterial);
        holder.tvMaterialName.setText(list.get(i).getName());
        holder.tvWeight.setText(list.get(i).getWeight());
        holder.tvPrice.setText(list.get(i).getPrice()+"");

        if (flag){

        }else {
            holder.imageBasketBuy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, LoginActivity.class));
                }
            });
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.image_material)
        ImageView imageMaterial;
        @BindView(R.id.tv_material_name)
        TextView tvMaterialName;
        @BindView(R.id.tv_weight)
        TextView tvWeight;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.image_basket_buy)
        ImageView imageBasketBuy;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
